package com.wtb.javatool.service.impl;


import com.wtb.javatool.dao.ResourceMapper;
import com.wtb.javatool.entity.Comment;
import com.wtb.javatool.entity.Favorite;
import com.wtb.javatool.entity.Resource;
import com.wtb.javatool.entity.ResourceType;
import com.wtb.javatool.service.IResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class ResourceService implements IResourceService {

    @Autowired
    ResourceMapper resourceMapper;

    @Override
    public List<Resource> selectResourceByCourseId(Integer course_id) {
        return resourceMapper.selectResourceByCourseId(course_id);
    }

    @Override
    public String selectResourceTypeById(Integer resource_type_id) {
        return resourceMapper.selectResourceTypeById(resource_type_id);
    }

    @Override
    public List<ResourceType> resourceTypeList() {
        return resourceMapper.resourceTypeList();
    }

    @Override
    public List<Resource> selectResourseByResourseTypeId(Integer resource_type_id,Integer course_id) {
        return resourceMapper.selectResourseByResourseTypeId(resource_type_id,course_id);
    }

    @Override
    public boolean uploadResource(String resource_name, Integer resource_type_id, byte[] resource_data, String size,Integer user_id,Integer course_id,String mimeType) {
        Resource resource = new Resource();
        resource.setResourceName(resource_name);
        resource.setResourceTypeId(resource_type_id); // 确保实体类字段是long类型
        resource.setResourceData(resource_data);
        resource.setResourceSize(size);
        resource.setUploadUserId(user_id);
        resource.setCourseId(course_id);
        resource.setMimeType(mimeType);
        // ...其他字段设置
        int result = resourceMapper.uploadResource(resource);
        Integer resourceId=null;
        if(result>0){
            resourceId = resource.getResourceId();
        }
        //插入关系表
        resourceMapper.addResourceTypeMapping(resourceId,resource_type_id);
        return result > 0;
    }

    @Override
    public Resource selectResourceDataByResourceId(Integer resource_id) {
        Resource data= resourceMapper.selectResourceDataByResourceId(resource_id);
        return data;
    }

    @Override
    public List<Resource> selectResourseByResourseTypeIdList(List<Integer> resourceTypeIds,Integer course_id) {
        if (resourceTypeIds == null || resourceTypeIds.isEmpty()) {
            return Collections.emptyList();  // 返回空集合而不是null
        }
        Integer typeCount = resourceTypeIds.size();
        return resourceMapper.selectResourseByResourseTypeIdList(resourceTypeIds,typeCount,course_id);

    }

    @Override
    public Integer saveresourceFavorite(Favorite favorite) {
        return resourceMapper.saveresourceFavorite(favorite);
    }

    @Override
    public List<Favorite> listAllResourceFavorite(Favorite favorite) {
        return resourceMapper.listAllResourceFavorite(favorite);
    }

    @Override
    public Integer saveResourceComment(Comment comment) {
        return resourceMapper.saveResourceComment(comment);
    }

    @Override
    public List<Comment> listAllResourceComment(Comment comment) {
        return resourceMapper.listAllResourceComment(comment);
    }

    @Override
    public Integer addLearnersCount(Integer resource_id) {
        return resourceMapper.addLearnersCount(resource_id);
    }

    @Override
    public Integer addCommentCount(Integer resource_id) {
        return resourceMapper.addCommentCount(resource_id);
    }

    @Override
    public Integer addTotalScore(Comment comment) {
        return resourceMapper.addTotalScore(comment);
    }

    @Override
    public Resource selectCommentCountandTotalScore(Integer resource_id) {
        return resourceMapper.selectCommentCountandTotalScore(resource_id);
    }

    @Override
    public Integer addScore(Resource resource) {
        return resourceMapper.addScore(resource);
    }

    @Override
    public Integer updateLikeCount(Integer resource_id) {
        return resourceMapper.updateLikeCount(resource_id);
    }

    @Override
    public String selectUserNameByUserId(Integer user_id) {
        return resourceMapper.selectUserNameByUserId(user_id);
    }

    @Override
    public InputStream downloadResource(Integer resource_id) throws Exception {
        // 1. 根据 resourceId 查询数据库记录
        Resource entity = resourceMapper.selectResourceDataByResourceId(resource_id);
        if (entity == null) {
            throw new Exception("资源不存在");
        }
        // 2. 从 BLOB 字段中提取二进制流（不同框架实现不同，以 MyBatis 为例）
         return new ByteArrayInputStream(entity.getResourceData());
    }

    @Override
    public List<Resource> selectResourceByName(String resource_name, Integer course_id) {
        return resourceMapper.selectResourceByName(resource_name,course_id);
    }
}
